package com.aliyun.heiyu.powermaster.server.dao.impl;

import com.aliyun.heiyu.common.util.DateUtil;
import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.common.util.StringUtil;
import com.aliyun.heiyu.powermaster.server.dao.WarningDao;
import com.aliyun.heiyu.powermaster.server.dao.entity.StateGridCollectInfoEntity;
import com.aliyun.heiyu.powermaster.server.dao.entity.WarningEntity;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class WarningDaoImpl implements WarningDao {

     String baseColumn="id,equipmentNumber,time,type,content,state,repairTime,deviceType,alarm_level";

    @Override
    public boolean insert(WarningEntity entity) {
        String sql = "insert into warning(equipmentNumber,time,type,content,state,deviceType,alarm_level) values (?,?,?,?,?,?,?)";
        Object[] kk = new Object[7];
        kk[0]=entity.getEquipmentNumber();
        kk[1]=entity.getTime();
        kk[2]=entity.getType();
        kk[3]=entity.getContent();
        kk[4]=entity.getState();
        kk[5]=entity.getDeviceType();
        kk[6]=entity.getAlarm_level();
        boolean result = JdbcUtil.update(sql, kk);
        return result;
    }

    @Override
    public List<WarningEntity> findAll(String state) {
        StringBuffer sqlBuffer = new StringBuffer("select "+baseColumn+" from warning");
        ArrayList<String> paramList = new ArrayList<>();
        Object[] params = null;
        if(StringUtil.isEmpty(state)){
            params=paramList.toArray();
            sqlBuffer.append(" order by id desc");
            String sql = sqlBuffer.toString();
            List<WarningEntity> result = JdbcUtil.queryForList(sql, params, WarningEntity.class);
            return result;
        }
        sqlBuffer.append(" where state = ? ");
        paramList.add(state);
        params=paramList.toArray();
        sqlBuffer.append("order by id desc");
        String sql = sqlBuffer.toString();
        List<WarningEntity> result = JdbcUtil.queryForList(sql, params, WarningEntity.class);
        return result;
    }

    @Override
    public boolean update(int id, String state) {
        String sql = "update warning set state = ?,repairTime = ? where id = ?";
        Object[] kk = new Object[3];
        kk[0]=state;
        kk[1]=DateUtil.getStringDate(new Date());
        kk[2]=id;
        boolean result = JdbcUtil.update(sql, kk);
        return result;
    }

    @Override
    public WarningEntity findOne(Integer id) {
        String sql="select "+baseColumn+" from warning where 1= ? and id = ?";
        Object[] kk = new Object[2];
        kk[0] = 1;
        kk[1] = id;
        WarningEntity result = JdbcUtil.queryForBean(sql, kk, WarningEntity.class);
        return result;
    }

    @Override
    public WarningEntity findByEqAndType(String equipmentNumber, String type) {
        String sql="select " + baseColumn + " from warning where equipmentNumber = ? and type = ? and state = 2 ";
        Object[] kk = new Object[2];
        kk[0] = equipmentNumber;
        kk[1] =type;
        WarningEntity result = JdbcUtil.queryForBean(sql, kk, WarningEntity.class);
        return result;
    }

    @Override
    public Map<String, String> findWarningCount() {
        String sql="select count(*) as count from warning where state = 2";
        Map<String, String> map = JdbcUtil.queryForMap(sql, null);
        return map;
    }

    @Override
    public List<StateGridCollectInfoEntity> findMaxServer() {
        Object[] kk = new Object[2];
        long l = System.currentTimeMillis();
        kk[0] = DateUtil.millisecondToDate(l - 60 * 1000);
        kk[1] = DateUtil.millisecondToDate(l);
        String sql="select server_sn,server_power from state_grid_collect_info where collect_time between ? and ? order by server_power desc";
        return JdbcUtil.queryForList(sql, kk, StateGridCollectInfoEntity.class);
    }

    @Override
    public boolean existWarn(String sn,String type) {
        if(StringUtil.isEmpty(type)){
            return true;
        }
        String sql = "select id from warning where equipmentNumber = ? and type = ? and state = 2 ";
        Object[] obj = new Object[2];
        obj[0] = sn;
        obj[1] = type;
        List<Map<String, String>> maps = JdbcUtil.queryForList_Map(sql, obj);
        return CollectionUtils.isEmpty(maps) ?false:true;
    }

    @Override
    public boolean updateWarningTime(int id) {
        String sql = "update warning set time = ? where id = ?";
        Object[] kk = new Object[2];
        kk[0]=DateUtil.getStringDate(new Date());
        kk[1]=id;
        boolean result = JdbcUtil.update(sql, kk);
        return result;
    }

    @Override
    public Map<String, String> findCurrentOnePower() {
        String sql="select id,server_sn,server_power,collect_time,cpu_usage from state_grid_collect_info order by id desc limit 1";
        Map<String, String> map = JdbcUtil.queryForMap(sql, null);
        return map;
    }

    @Override
    public Map<String, String> findSetAlarmLevel() {
        String sql="select set_alarm_level from warning_config where id = 1";
        Map<String, String> map = JdbcUtil.queryForMap(sql, null);
        return map;
    }

    @Override
    public boolean updateSetAlarmLevel(String level) {
        String sql = "update warning_config set set_alarm_level = ? where id = ?";
        Object[] kk = new Object[2];
        kk[0]=level;
        kk[1]=1;
        boolean result = JdbcUtil.update(sql, kk);
        return result;
    }

}
